package com.awesome.videodemo.video.natives;

import androidx.core.content.FileProvider;

import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.View;

import com.awesome.common.base.mvp.BaseActivity;
import com.awesome.common.constant.Constant;
import com.awesome.videodemo.R;

import java.io.File;

/**
 * 手机原生播放器应用播放
 */
public class NativeActivity extends BaseActivity {
    private File mFile;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_native_avtivity);
        if (!hasPermission(Constant.WRITE_EXTERNAL_PERMISSION)) {
            requestPermission(Constant.WRITE_EXTERNAL_CODE, Constant.WRITE_EXTERNAL_PERMISSION);
        }
        mFile = new File("/storage/emulated/0/DCIM/Camera/TG-2022-06-22-230432884.mp4");
        nativePlayer();
    }

    private void nativePlayer() {
        findViewById(R.id.tv_native_player).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_VIEW);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                    intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                    Uri uri = FileProvider.getUriForFile(NativeActivity.this, getPackageName() + ".fileprovider", mFile);
                    intent.setDataAndType(uri, "video/*");
                } else {
                    intent.setDataAndType(Uri.fromFile(mFile), "video/*");
                }
                startActivity(intent);
            }
        });
    }
}